import time

from sqlalchemy.orm import Session
from models.match import Match
from schemas.match import MatchUpdate
from typing import Optional, List


def create(db: Session, obj_in: dict) -> Match:
    db_obj = Match(**obj_in)
    db.add(db_obj)
    db.commit()
    db.refresh(db_obj)
    return db_obj

def get(db: Session, id: int) -> Optional[Match]:
    return db.query(Match).filter(Match.id == id).first()

def get_with_times(db: Session, start_time:int ,end_time: int) -> Optional[Match]:
    return db.query(Match).order_by(Match.id.desc()).filter(Match.start_time >=start_time).filter(Match.start_time < end_time).all()

#获取3小时内的比赛，并且没有显示比分的
def get_no_scores(db: Session) -> List[Match]:
    return (
        db.query(Match)
        .filter(Match.score.is_(None))
        .filter(time.time()*1000-Match.start_time <108000000
        )
        .all()
    )

def get_multi( db: Session, *, skip: int = 0, limit: int = 100) -> List[Match]:
    return db.query(Match).order_by(Match.id.desc()).offset(skip).limit(limit).all()

#只更新比分和输赢
def update(db: Session, match_id: int, match: MatchUpdate) -> Match:
    r = db.query(Match).filter_by(id=match_id).update({'score':match.score,'performance':match.performance})
    db.commit()
    return r

def delete(db: Session, id: int) -> Match:
    db_obj = get(db,id)
    db.delete(db_obj)
    db.commit()
    return db_obj


def get_performance(score,prediction):
    performance = ''
    pre = prediction.split(' ')
    home_score = int(score.split('-')[0])
    away_score = int(score.split('-')[1])
    zf = home_score + away_score
    match pre[0]:
        case 'Over':
            if zf - float(pre[1]) == 0:
                performance = ('Zou shui')
            if zf - float(pre[1]) == 0.25:
                performance = ('Win Half')
            if zf - float(pre[1]) > 0.49:
                performance = ('Win')
            if zf - float(pre[1]) == -0.25:
                performance = ('Lose Half')
            if zf - float(pre[1]) < -0.49:
                performance = ('Lose')
        case 'Under':
            if zf - float(pre[1]) == 0:
                performance = ('Zou shui')
            if zf - float(pre[1]) == -0.25:
                performance = ('Win Half')
            if zf - float(pre[1]) < -0.49:
                performance = ('Win')
            if zf - float(pre[1]) == 0.25:
                performance = ('Half Lose')
            if zf - float(pre[1]) > 0.49:
                performance = ('Lose')
        case 'Home':
            if home_score + float(pre[1]) - away_score == 0:
                performance = ('Zou shui')
            if home_score + float(pre[1]) - away_score == 0.25:
                performance = ('Half Win')
            if home_score + float(pre[1]) - away_score > 0.49:
                performance = ('Win')
            if home_score + float(pre[1]) - away_score == -0.25:
                performance = ('Half Lose')
            if home_score + float(pre[1]) - away_score < -0.49:
                performance = ('Lose')
        case 'Away':
            if away_score + float(pre[1]) - away_score == 0:
                performance = ('Zou shui')
            if away_score + float(pre[1]) - away_score == 0.25:
                performance = ('Half Win')
            if away_score + float(pre[1]) - away_score > 0.49:
                performance = ('Win')
            if away_score + float(pre[1]) - away_score == -0.25:
                performance = ('Half Lose')
            if away_score + float(pre[1]) - away_score < -0.49:
                performance = ('Lose')
    return performance

if __name__ == "__main__":
    import hashlib

    def generate_hash(home: str, away: str, prediction: str, random: str) -> str:
        raw = f"{home}|{away}|{prediction}|{random}|"
        return hashlib.sha256(raw.encode()).hexdigest()


    from datetime import datetime
    from schemas.match import MatchCreate



    from utils.database import SessionLocal

    db = SessionLocal()
    match = MatchUpdate
    match.id = 273
    score = '1-4'
    match = get(db,match.id)
    match.score = score

    match.performance = get_performance(score,match.prediction)

    b = update(db,match.id ,match)
    print(b)
